package finedev.android.venue;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.EditText;

public class NetworkManager extends AsyncTask<String, Void, Integer> {
	
	private final String TAG = "network";
	
	private NetworkActivity mNetworkActivity;
	private ProgressDialog mProgressDialog;
	private String mMethod;
	private String mResultString;
	
	public NetworkManager(Activity activity, ProgressDialog progressDialog, String method )
	{
		mNetworkActivity = (NetworkActivity) activity;
		mProgressDialog = progressDialog;
		mMethod = method;
	}
	
	@Override
	protected void onPreExecute() {
		mProgressDialog.show();
	}

	@Override
	protected Integer doInBackground(String... arg0) 
	{
		int retCode = 0;
		
		HttpClient httpClient;
		HttpPost httpPost;
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
		HttpResponse httpResponse;
		HttpEntity resHttpEntity;
		
    	try {
    		httpClient = new DefaultHttpClient();
			httpPost = new HttpPost(arg0[0]);
			if ( mMethod.equals("login") ) {
				nameValuePairs.add(new BasicNameValuePair("login_type", arg0[1]));
				nameValuePairs.add(new BasicNameValuePair("user_id", arg0[2]));
				nameValuePairs.add(new BasicNameValuePair("passwd", arg0[3]));
			} else if ( mMethod.equals("facebook_check") ) {
				nameValuePairs.add(new BasicNameValuePair("login_type", arg0[1]));
				nameValuePairs.add(new BasicNameValuePair("facebook_user_id", arg0[2]));
			} else if ( mMethod.equals("join") ) {
				nameValuePairs.add(new BasicNameValuePair("reg_id", arg0[1]));
				nameValuePairs.add(new BasicNameValuePair("facebook_id", arg0[2]));
				nameValuePairs.add(new BasicNameValuePair("reg_email", arg0[3]));
				nameValuePairs.add(new BasicNameValuePair("reg_name", arg0[4]));
				nameValuePairs.add(new BasicNameValuePair("reg_passwd", arg0[5]));
				nameValuePairs.add(new BasicNameValuePair("reg_passwd2", arg0[6]));
				nameValuePairs.add(new BasicNameValuePair("genre", arg0[7]));
			} else if ( mMethod.equals("newvideo") ) {
				nameValuePairs.add(new BasicNameValuePair("max_ad_key", arg0[1]));
				nameValuePairs.add(new BasicNameValuePair("count", arg0[2]));
			}
			httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
			
			httpResponse = httpClient.execute( httpPost );
			resHttpEntity = httpResponse.getEntity();
			retCode = httpResponse.getStatusLine().getStatusCode();
			mResultString = EntityUtils.toString(resHttpEntity);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return retCode;
	}
	
	@Override
	protected void onPostExecute(Integer retCode) {
		mProgressDialog.dismiss();
		if ( retCode >= 200 && retCode < 300 ) {
			mNetworkActivity.ReceiveSuccess( mResultString );
		} else {
			mNetworkActivity.ReceiveFail( "fail" );
		}
	}

}

abstract class NetworkActivity extends Activity implements NetworkInterface{}

interface NetworkInterface
{
	abstract void ReceiveSuccess(String str);
	abstract void ReceiveFail(String str); 
}